package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApArticleDislikeMapper;
import com.heima.behavior.service.ApArticleDislikeService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.behavior.pojos.ApArticleDislike;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 文章不喜欢行为数据业务逻辑层
 *
 * @author huangjianfei
 * @date 2024/11/09
 */
@Service
@RequiredArgsConstructor
public class ApArticleDislikeServiceImpl extends ServiceImpl<ApArticleDislikeMapper, ApArticleDislike> implements ApArticleDislikeService {

    private final CacheService cacheService;

    private final ApArticleDislikeMapper apArticleDislikeMapper;

    /**
     * 不喜欢文章 or 取消不喜欢
     * @param dto
     * @return
     */
    @Override
    public ResponseResult dislikeApArticle(UnLikesBehaviorDto dto) {
        // 1、检查是否登录
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 2、检查参数
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 3、添加到Db
        return saveDislikeRecordToDb(dto, user.getId());
    }

    private ResponseResult saveDislikeRecordToDb(UnLikesBehaviorDto dto, Integer userId) {
        // 1、查询Db中有没有记录
        ApArticleDislike apArticleDislike = apArticleDislikeMapper.selectByArticleIdAndUserId(dto.getArticleId(), userId);
        ApArticleDislike dislike = new ApArticleDislike();
        dislike.setArticleId(dto.getArticleId());
        dislike.setUserId(userId);
        dislike.setType(dto.getType());
        dislike.setDislikeTimestamp(new Date());
        if (apArticleDislike == null) {
            apArticleDislikeMapper.insertApArticleDislike(dislike);
        } else {
            if (dto.getType().equals(apArticleDislike.getType())) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"请勿重复操作");
            }
            apArticleDislikeMapper.updateApArticleDislike(dislike);
        }
        return saveDislikeRecordToRedis(dislike.getArticleId(), dislike.getUserId(), dislike.getType());
    }

    private ResponseResult saveDislikeRecordToRedis(Long articleId, Integer userId, Short type) {
        String dislikeKey = BehaviorConstants.ARTICLE_DISLIKE + "_" + articleId;
        Object o = cacheService.hGet(dislikeKey, String.valueOf(userId));
        if (o != null) {
            Short aShort = Short.valueOf((String) o);
            if (type.equals(aShort)) { // 相同直接返回
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        }
        // 2、添加到redis
        cacheService.hPut(dislikeKey, String.valueOf(userId), String.valueOf(type));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
